/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.punish.helpmanage.service.impl;

import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.punish.helpmanage.dto.HelpTypeManageDelDto;
import com.icinfo.cloud.provider.punish.helpmanage.dto.HelpTypeManageDto;
import com.icinfo.cloud.provider.punish.helpmanage.dto.HelpTypeManagePageRequest;
import com.icinfo.cloud.provider.punish.helpmanage.mapper.HelpTypeManageMapper;
import com.icinfo.cloud.provider.punish.helpmanage.model.HelpTypeManage;
import com.icinfo.cloud.provider.punish.helpmanage.service.IHelpTypeManageService;
import com.icinfo.cloud.provider.punish.helpmanage.vo.HelpTypeManageVo;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.mybatis.pagehelper.PageHelper;
import com.icinfo.framework.tools.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 描述:  help_type_manage 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年12月11日
 */
@Service
public class HelpTypeManageServiceImpl extends MyBatisServiceSupport implements IHelpTypeManageService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(HelpTypeManageServiceImpl.class);

    @Autowired
    private HelpTypeManageMapper helpTypeManageMapper;

    @Override
    public List<HelpTypeManageVo> getPageList(HelpTypeManagePageRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getLength(), request.getOrderBy());
        List<HelpTypeManageVo> result = helpTypeManageMapper.getList(request);
        return result;
    }
    @Override
    public List<HelpTypeManageVo> getList() {
        HelpTypeManagePageRequest request = new HelpTypeManagePageRequest();
        List<HelpTypeManageVo> result = helpTypeManageMapper.getList(request);
        return result;
    }

    @Override
    public int delete(HelpTypeManageDelDto dto) {
        int count = 0;
        //判断是否可以删除 - 删除帮扶类型时需要判断该类型有无在帮扶对象管理功能中使用了，若已被使用则无法删除。
        int isExistInHelpManage  = helpTypeManageMapper.selectHelpManageCount(dto.getId());
        if(isExistInHelpManage > 0){
            return count;
        }
        count = helpTypeManageMapper.deleteByPrimaryKey(dto.getId());
        return count ;
    }

    @Override
    public int addOrUpdate(HelpTypeManageDto dto) {
        int count = 0 ;

        UserDTO userInfo = UserUtil.getUserInfo();
        if(StringUtils.isBlank(dto.getId())){
            HelpTypeManage manage = new HelpTypeManage();
            BeanUtils.copyProperties(dto,manage);
            //新增
            manage.setId(StringUtils.uuid());
            manage.setCreateTime(new Date());
            manage.setCreateUserId(userInfo.getUserId().toString());
            manage.setCreateUserName(userInfo.getRealName());
            manage.setUpdateTime(new Date());
            manage.setUpdateUserId(userInfo.getUserId().toString());
            manage.setUpdateUserName(userInfo.getRealName());
            manage.setIsDelete(Constant.STRING_0);
            count = helpTypeManageMapper.insert(manage);
        }else{
            //修改
            HelpTypeManage newHelpTypeManage = new HelpTypeManage();
            //修改签字确认状态
            Example updateExample = new Example(HelpTypeManage.class);
            updateExample.createCriteria().
                    andEqualTo("id", dto.getId());
            BeanUtils.copyProperties(dto,newHelpTypeManage);
            newHelpTypeManage.setUpdateTime(new Date());
            newHelpTypeManage.setUpdateUserId(userInfo.getUserId().toString());
            newHelpTypeManage.setUpdateUserName(userInfo.getRealName());
            count = helpTypeManageMapper.updateByExampleSelective(newHelpTypeManage, updateExample);
        }
        return count;
    }

    @Override
    public HelpTypeManageVo getHelpTypeManageInfo(String id) {
        HelpTypeManageVo result = new HelpTypeManageVo();
        HelpTypeManage helpTypeManage = helpTypeManageMapper.selectByPrimaryKey(id);
        if( helpTypeManage == null){
            return null;
        }
        BeanUtils.copyProperties(helpTypeManage,result);
        return result;
    }
}